package com.midea.ai.air.ui.user;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.utils.StringUtil;

/**
 * Created by guojun3 on 2016-3-19.
 */
public class ForgetPasswordActivity extends BaseActivity implements View.OnClickListener {


    private EditText Ed_phone,Ed_verification_code;
    private Button bt_next,bt_commit;
    private TextView mTextView,mTV_Error;
    private ImageView phone_img,vc_img;

    private Dialog send_vc_Dialog;
    private Dialog commit_vc_Dialog;

    private static final int SUCCESS_VERIFICATION_CODE = 0X110;
    private static final int SUCCESS_SEND_VERIFICATION_CODE = 0X111;

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case SUCCESS_VERIFICATION_CODE:
                    if(commit_vc_Dialog != null){
                        commit_vc_Dialog.dismiss();
                    }
                    startActivity(new Intent(ForgetPasswordActivity.this,SetPasswordActivity.class));
                    break;
                case SUCCESS_SEND_VERIFICATION_CODE:
                    if(send_vc_Dialog != null){
                        send_vc_Dialog.dismiss();
                    }


                    mTextView.setText(R.string.log_in_from_verification_code_send);
                    Ed_phone.setVisibility(View.GONE);
                    bt_next.setVisibility(View.GONE);
                    Ed_verification_code.setVisibility(View.VISIBLE);
                    bt_commit.setVisibility(View.VISIBLE);
                    bt_commit.setAlpha(0.25f);
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpassword);

        initView();
    }

    private void initView() {
        findViewById(R.id.forget_password_back).setOnClickListener(this);
        Ed_phone = (EditText) findViewById(R.id.forget_password_Phone);
        Ed_verification_code = (EditText) findViewById(R.id.forget_password_verification);
        bt_next = (Button)findViewById(R.id.forget_password_bt_next);
        bt_commit = (Button)findViewById(R.id.forget_password_bt);
        mTextView = (TextView)findViewById(R.id.forget_password_top);
        mTV_Error = (TextView)findViewById(R.id.forget_password_top_error);
        phone_img = (ImageView)findViewById(R.id.forgetPassword_imageView_phone);
        vc_img = (ImageView)findViewById(R.id.forgetPassword_imageView_verification_code);

        bt_commit.setOnClickListener(this);
        bt_next.setOnClickListener(this);
        creatDialog();
        bt_next.setAlpha(0.25f);


        send_vc_Dialog = LoginActivity.createLoadingDialog(ForgetPasswordActivity.this,R.string.dialog_send_verification_code);
        commit_vc_Dialog = LoginActivity.createLoadingDialog(ForgetPasswordActivity.this,R.string.dialog_submit_verification_code);

        //忘记密码下一步按钮激活
        Ed_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                LoginActivity.mLog("beforeTextChanged = " + s.toString() + "::" + start + "::" + count + "::" + after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                LoginActivity.mLog("onTextChanged = " + s.toString() + "::" + start + "::" + before + "::" + count);
                if (count < 0) {
                    bt_next.setAlpha(0.25f);
                } else {
                    bt_next.setAlpha(1f);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                LoginActivity.mLog("afterTextChanged = " + s.toString());
            }
        });


        //验证码按钮激活
        Ed_verification_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                LoginActivity.mLog("beforeTextChanged = " + s.toString() + "::" + start + "::" + count + "::" + after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                LoginActivity.mLog("onTextChanged = " + s.toString() + "::" + start + "::" + before + "::" + count);
                if (count < 0) {
                    bt_commit.setAlpha(0.25f);
                } else {
                    bt_commit.setAlpha(1f);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                LoginActivity.mLog("afterTextChanged = " + s.toString());
            }
        });


        //输入框焦点，判断是否显示图标
        Ed_phone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    phone_img.setVisibility(View.VISIBLE);
                } else {
                    phone_img.setVisibility(View.INVISIBLE);
                }
            }
        });
        Ed_verification_code.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    vc_img.setVisibility(View.VISIBLE);
                } else {
                    vc_img.setVisibility(View.INVISIBLE);
                }
            }
        });



    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.forget_password_back:
                //back
                finish();
                break;
            case R.id.forget_password_bt_next:
                //下一步
                password_Verification_code();
                break;
            case R.id.dialog_item_textView_cancel:
                //dialog取消按钮
                forget_next_dialog.dismiss();

                break;
            case R.id.dialog_item_textView_confirm:
                //dialog确定按钮
                if(forget_next_dialog != null) {
                    forget_next_dialog.dismiss();
                }
                send_vc_Dialog.show();
                mHandler.sendEmptyMessageDelayed(SUCCESS_SEND_VERIFICATION_CODE, 1000);
                break;
            case R.id.forget_password_bt:
                //提交验证码

                writer_verification();
                break;

            default:
                break;
        }

    }

    /**
     * 通过发送短信验证码进行下一步
     */
    private void password_Verification_code(){
        mTextView.setText(R.string.log_in_from_verification_code);
        Ed_phone.setVisibility(View.VISIBLE);
        bt_next.setVisibility(View.VISIBLE);
        Ed_verification_code.setVisibility(View.GONE);
        bt_commit.setVisibility(View.GONE);

        String phone = Ed_phone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            mTV_Error.setText(R.string.log_in_phone_empty);
            return;
        }

        if (phone.length() != 11) {
            mTV_Error.setText(R.string.log_in_phone_eleven);
            return;
        }

        if (!StringUtil.isMobileNO(phone)) {
            mTV_Error.setText(R.string.log_in_phone_error);
            return;
        }

        mTV_Error.setText("");
        if(forget_next_dialog != null){
            dialog_show_phone.setText("+86  "+phone);
            forget_next_dialog.show();
        }

    }


    /**
     * 填写提交验证码设置密码
     */
    private void writer_verification(){
        String verification_code = Ed_verification_code.getText().toString().trim();
        if (TextUtils.isEmpty(verification_code)) {
            mTV_Error.setText(R.string.top_verification_code_empty);
            return;
        }

        commit_vc_Dialog.show();
        mHandler.sendEmptyMessageDelayed(SUCCESS_VERIFICATION_CODE,1800);

    }


    private Dialog forget_next_dialog;
    private TextView dialog_show_phone;
    private void creatDialog(){
        View view_forget_password = LayoutInflater.from(ForgetPasswordActivity.this)
                .inflate(R.layout.dialog_item, null);
        forget_next_dialog = new Dialog(ForgetPasswordActivity.this, R.style.my_activate_dialog);
        forget_next_dialog.setCancelable(true);
        forget_next_dialog.setCanceledOnTouchOutside(false);
        forget_next_dialog.setContentView(view_forget_password);
        //dialog_show_phone = (TextView) view_forget_password.findViewById(R.id.dialog_item_textView_content_two);
        TextView dialog_cancel = (TextView) view_forget_password.findViewById(R.id.dialog_item_textView_cancel);
        TextView dialog_confirm = (TextView) view_forget_password.findViewById(R.id.dialog_item_textView_confirm);
       // dialog_show_phone.setOnClickListener(this);
        dialog_cancel.setOnClickListener(this);
        dialog_confirm.setOnClickListener(this);
    }
}
